﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Code;

namespace SBACN.WebSite.UC
{
    /*
    public partial class Pager : BaseControl
    {
        public int PageSize { get; set; }
        public int PageIndex { get; set; }
        public int RecordCount { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitCtrl();
            }
        }

        protected void InitCtrl()
        {

        }

        protected void lbtnFirstPage_Click(object sender, EventArgs e)
        {

        }

        protected void lbtnLastPage_Click(object sender, EventArgs e)
        {

        }


        protected void lbtnPageIndex_Click(object sender, EventArgs e)
        {

        }
    }
    */

	public partial class Pager : BaseControl
    {
        /// <summary>
        /// 首尾显示的页码数量,默认为2
        /// </summary>
        public int FirstLast_rpt_Size
        {
            get
            {
                if (ViewState["FirstLast_rpt_Size"] != null)
                {
                    return Int32.Parse(ViewState["FirstLast_rpt_Size"].ToString());
                }
                return 2;
            }
            set
            {
                ViewState["FirstLast_rpt_Size"] = value.ToString();
            }
        }
        /// <summary>
        /// 中部显示的页码数量,默认为5
        /// </summary>
        public int Middle_rpt_Size
        {
            get
            {
                if (ViewState["Middle_rpt_Size"] != null)
                {
                    return Int32.Parse(ViewState["Middle_rpt_Size"].ToString());
                }
                return 5;
            }
            set
            {
                ViewState["Middle_rpt_Size"] = value.ToString();
            }
        }
        /// <summary>
        /// 当前页码
        /// </summary>
        public int CurrentPageIndex
        {
            get
            {
                if (ViewState["CurrentPageIndex"] != null)
                {
                    return Int32.Parse(ViewState["CurrentPageIndex"].ToString());
                }
                return 1;
            }
            set
            {
                ViewState["CurrentPageIndex"] = value.ToString();
            }
        }
        /// <summary>
        /// 下一页的页码
        /// </summary>
        public int NextIndex
        {
            get
            {
                if (ViewState["NextIndex"] != null)
                {
                    return Int32.Parse(ViewState["NextIndex"].ToString());
                }
                return 2;
            }
            set
            {
                ViewState["NextIndex"] = value.ToString();
            }
        }
        /// <summary>
        /// 记录总数
        /// </summary>
        public int RecordCount
        {
            get
            {
                if (ViewState["RecordCount"] != null)
                {
                    return int.Parse(ViewState["RecordCount"].ToString());
                }
                return 0;
            }
            set
            {
                ViewState["RecordCount"] = value.ToString();
            }
        }
        /// <summary>
        /// 页码总数
        /// </summary>
        protected int PageCount
        {
            get
            {
                if (ViewState["PageCount"] != null)
                {
                    return int.Parse(ViewState["PageCount"].ToString());
                }
                return 0;
            }
            set
            {
                ViewState["PageCount"] = value.ToString();
            }
        }
        /// <summary>
        /// 省略号前后各保留多少页码,默认为6
        /// </summary>
        public int SizeToEllipsis
        {
            get
            {
                if (ViewState["SizeToEllipsis"] != null)
                {
                    return Int32.Parse(ViewState["SizeToEllipsis"].ToString());
                }
                return 6;
            }
            set
            {
                ViewState["SizeToEllipsis"] = value.ToString();
            }
        }
        /// <summary>
        /// 每页显示的记录数,默认为10
        /// </summary>
        public int PageSize
        {
            get
            {
                if (ViewState["PageSize"] != null)
                {
                    return int.Parse(ViewState["PageSize"].ToString());
                }
                return 10;
            }
            set
            {
                ViewState["PageSize"] = value.ToString();
            }
        }
        /// <summary>
        /// 默认为4
        /// </summary>
        private int MaxToEllipsis
        {
            get
            {
                if (ViewState["MaxToEllipsis"] != null)
                {
                    return Int32.Parse(ViewState["MaxToEllipsis"].ToString());
                }
                return 4;
            }
            set
            {
                ViewState["MaxToEllipsis"] = value.ToString();
            }
        }
        private bool _showPageStatus = true;
        /// <summary>
        /// 是否显示[第x页/共x页]
        /// </summary>
        public bool ShowPageStatus
        {
            get
            {
                //if (ViewState["ShowPageStatus"] != null)
                //{
                //    return bool.Parse(ViewState["ShowPageStatus"].ToString());
                //}
                return _showPageStatus;
            }
            set
            {
                _showPageStatus = value;
            }
        }

        public delegate void _PageIndexChanged(int CurrentPageIndex, int NextPageIndex);

        public event _PageIndexChanged SelectedPageIndexChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
            }
        }

        public void BindData()
        {
            lblPageStatus.Text = "第" + CurrentPageIndex.ToString() + "页" + "&nbsp;/&nbsp;共" + PageCount.ToString() + "页";
            lblPageStatus.Visible = ShowPageStatus;
            #region 激活或停用 "上一页","下一页"
            if (CurrentPageIndex == 1)
            {
                LbtFirstPage.Enabled = false;
                LbtFirstPage.CssClass = "cutepage prev";
            }
            else
            {
                LbtFirstPage.Enabled = true;
                LbtFirstPage.CssClass = LbtFirstPage.CssClass.Replace("cutepage prev", "");
            }
            if (CurrentPageIndex == PageCount)
            {
                LbtLastPage.Enabled = false;
                LbtLastPage.CssClass = "cutepage next";
            }
            else
            {
                LbtLastPage.Enabled = true;
                LbtLastPage.CssClass = LbtLastPage.CssClass.Replace("cutepage next", "");
            }
            #endregion

            #region 页码 < (2*FL+M) Size
            if (PageCount <= (FirstLast_rpt_Size * 2) + Middle_rpt_Size)
            {
                //前后都隐藏
                this.RptFirst.Visible = false;
                this.Lit_first.Visible = false;
                this.Lit_last.Visible = false;
                this.RptLast.Visible = false;

                //绑定中部
                List<Pages> pages = new List<Pages>();
                for (int i = 1; i <= PageCount; i++)
                {
                    Pages p = new Pages();
                    p.PageIndex = i;
                    pages.Add(p);
                }
                BindRepeater(this.RptMiddle, pages);
            }
            #endregion 页码 < (2*FL+M) Size

            #region 页码 > (2*FL+M) Size
            if (PageCount > ((FirstLast_rpt_Size * 2) + Middle_rpt_Size))
            {
                #region 显示规则为：oxx
                if (CurrentPageIndex < SizeToEllipsis)
                {
                    //前面都隐藏
                    this.RptFirst.Visible = false;
                    this.Lit_first.Visible = false;
                    this.RptLast.Visible = true;
                    this.Lit_last.Visible = true;

                    #region 绑定中部
                    List<Pages> pages = new List<Pages>();
                    int temp = SizeToEllipsis;
                    if (CurrentPageIndex < MaxToEllipsis)
                    {
                        temp = MaxToEllipsis;
                    }
                    else
                    {
                        temp = CurrentPageIndex + 1;
                        if (temp > SizeToEllipsis)
                            temp = temp - 1;
                    }

                    for (int i = 1; i <= temp; i++)
                    {
                        Pages p = new Pages();
                        p.PageIndex = i;
                        pages.Add(p);
                    }

                    BindRepeater(this.RptMiddle, pages);
                    #endregion 绑定中部

                    #region 绑定后面
                    pages.Clear();
                    temp = PageCount - FirstLast_rpt_Size + 1;
                    for (int i = temp; i <= PageCount; i++)
                    {
                        Pages p = new Pages();
                        p.PageIndex = i;
                        pages.Add(p);
                    }
                    BindRepeater(this.RptLast, pages);
                    #endregion 绑定后面
                    return;
                }
                #endregion

                #region 显示规则为：xxx
                if (CurrentPageIndex >= SizeToEllipsis && (CurrentPageIndex <= (PageCount - SizeToEllipsis + 1)))
                {
                    this.RptFirst.Visible = true;
                    this.Lit_first.Visible = true;
                    this.RptLast.Visible = true;
                    this.Lit_last.Visible = true;
                    #region 绑定前面
                    List<Pages> pages = new List<Pages>();
                    for (int i = 1; i <= FirstLast_rpt_Size; i++)
                    {
                        Pages p = new Pages();
                        p.PageIndex = i;
                        pages.Add(p);
                    }
                    BindRepeater(this.RptFirst, pages);
                    #endregion 绑定前面

                    #region 绑定中部
                    pages.Clear();
                    int temp = CurrentPageIndex - (Middle_rpt_Size / 2);
                    for (int i = temp; i <= (CurrentPageIndex + (Middle_rpt_Size / 2)); i++)
                    {
                        Pages p = new Pages();
                        p.PageIndex = i;
                        pages.Add(p);
                    }
                    BindRepeater(this.RptMiddle, pages);
                    #endregion 绑定中部

                    #region 绑定后面
                    pages.Clear();
                    temp = PageCount - FirstLast_rpt_Size + 1;
                    for (int i = temp; i <= PageCount; i++)
                    {
                        Pages p = new Pages();
                        p.PageIndex = i;
                        pages.Add(p);
                    }
                    BindRepeater(this.RptLast, pages);
                    #endregion 绑定后面
                    return;
                }
                #endregion

                #region 显示规则为：xxo
                if (CurrentPageIndex > (PageCount - SizeToEllipsis))
                {
                    //后面都隐藏
                    this.RptFirst.Visible = true;
                    this.Lit_first.Visible = true;
                    this.RptLast.Visible = false;
                    this.Lit_last.Visible = false;

                    #region 绑定中部
                    List<Pages> pages = new List<Pages>();
                    int temp = SizeToEllipsis;
                    if (CurrentPageIndex > (PageCount - MaxToEllipsis))
                    {
                        temp = PageCount - MaxToEllipsis + 1;
                        if (temp == CurrentPageIndex)
                            temp = temp - 1;
                    }
                    else
                    {
                        temp = CurrentPageIndex - 1;
                        if (temp < (PageCount - SizeToEllipsis))
                            temp = CurrentPageIndex + 1;
                    }

                    for (int i = temp; i <= PageCount; i++)
                    {
                        Pages p = new Pages();
                        p.PageIndex = i;
                        pages.Add(p);
                    }
                    BindRepeater(this.RptMiddle, pages);
                    #endregion 绑定中部

                    #region 绑定前面
                    pages.Clear();
                    for (int i = 1; i <= FirstLast_rpt_Size; i++)
                    {
                        Pages p = new Pages();
                        p.PageIndex = i;
                        pages.Add(p);
                    }
                    BindRepeater(this.RptFirst, pages);
                    #endregion 绑定前面
                }
                #endregion
            }
            #endregion 页码 < (2*FL+M) Size
        }
        private void TurnPage(int pageIndex)
        {
            NextIndex = pageIndex;
            SelectedPageIndexChanged(CurrentPageIndex, NextIndex);
            this.CurrentPageIndex = NextIndex;
            BindData();
        }
        protected void RptFirst_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Pages p = e.Item.DataItem as Pages;
                if (p.PageIndex == CurrentPageIndex)
                {
                    LinkButton Lbt = e.Item.FindControl("LbtFisrt") as LinkButton;
                    Lbt.CssClass = "selected";
                    Lbt.Enabled = false;
                }
            }
        }
        protected void RptMiddle_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Pages p = e.Item.DataItem as Pages;
                if (p.PageIndex == CurrentPageIndex)
                {
                    LinkButton Lbt = e.Item.FindControl("LbtMiddle") as LinkButton;
                    Lbt.CssClass = "selected";
                    Lbt.Enabled = false;
                }
            }
        }
        protected void RptLast_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Pages p = e.Item.DataItem as Pages;
                if (p.PageIndex == CurrentPageIndex)
                {
                    LinkButton Lbt = e.Item.FindControl("LbtLast") as LinkButton;
                    Lbt.CssClass = "selected";
                    Lbt.Enabled = false;
                }
            }
        }
        protected void RptFirst_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("turnpage"))
                TurnPage(int.Parse(e.CommandArgument.ToString()));
        }
        protected void RptMiddle_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("turnpage"))
                TurnPage(int.Parse(e.CommandArgument.ToString()));
        }
        protected void RptLast_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName.Equals("turnpage"))
                TurnPage(int.Parse(e.CommandArgument.ToString()));
        }
        protected void LbtFirstPage_Click(object sender, EventArgs e)
        {
            TurnPage(this.CurrentPageIndex - 1);
        }
        protected void LbtLastPage_Click(object sender, EventArgs e)
        {
            TurnPage(this.CurrentPageIndex + 1);
        }
        private void BindRepeater(Repeater rpt, List<Pages> pages)
        {
            rpt.DataSource = pages;
            rpt.DataBind();
        }

        public void InitControl()
        {
            //不需要分页时不显示分页控件
            if (RecordCount / PageSize < 1)
            {
                #region Visible = false;
                lblPageStatus.Visible = false;
                LbtFirstPage.Visible = false;
                LbtLastPage.Visible = false;
                Lit_first.Visible = false;
                Lit_last.Visible = false;
                RptFirst.Visible = false;
                RptMiddle.Visible = false;
                RptLast.Visible = false;
                #endregion
                return;
            }
            else
            {
                #region Visible = true;
                lblPageStatus.Visible = ShowPageStatus;
                LbtFirstPage.Visible = true;
                LbtLastPage.Visible = true;
                Lit_first.Visible = true;
                Lit_last.Visible = true;
                RptFirst.Visible = true;
                RptMiddle.Visible = true;
                RptLast.Visible = true;
                #endregion
            }

            if (RecordCount % PageSize == 0)
            {
                PageCount = RecordCount / PageSize;
            }
            else
            {
                PageCount = (RecordCount / PageSize) + 1;
            }

            BindData();
        }
    }

    public class Pages
    {
        private int _pageIndex;
        public int PageIndex
        {
            get
            {
                return _pageIndex;
            }
            set
            {
                _pageIndex = value;
            }
        }
    }
}